package com.it.controller;

import com.it.domain.Student;
import com.it.service.StudentService;

import java.util.List;
import java.util.Scanner;

public class StudentController {

    private Scanner sc = new Scanner(System.in);

    private StudentService studentService = new StudentService();


    //修改学生信息
    public void updateStudent() {
        System.out.println("请输入要修改的学生id:");
        String id = sc.next();
        if (studentService.isExist(id)) {
            Student student = getStudent(id);
            // 调用service修改
            boolean ret = studentService.updateStudent(student);
            if (ret) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } else {
            System.out.println("该id不存在");
        }
    }

    //封装学生信息
    private Student getStudent(String id) {
        System.out.print("请输入学生姓名:");
        String name = sc.next();
        System.out.print("请输入学生年龄:");
        String age = sc.next();
        System.out.print("请输入学生性别:");
        String gender = sc.next();
        System.out.print("请输入学生专业:");
        String major = sc.next();
        // 封装数据
        return new Student(id, name, age, gender, major);
    }

    //删除学生信息
    public void deleteStudent() {
        System.out.println("请输入要删除的学生id:");
        String id = sc.next();
        // 调用service删除
        boolean ret = studentService.deleteStudent(id);
        if (ret) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    // 查询全部学生信息
    public void listStudent() {
        // 调用service查询
        List<Student> studentList = studentService.findAll();
        System.out.println("学号\t姓名\t年龄\t性别\t专业");
        // 遍历
        for (Student student : studentList) {
            System.out.println(student.getStudentId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getMajor());
        }
    }

    public void addStudent() {
        // 提示用户输入id
        System.out.println("请输入学生id:");
        String id = sc.next();
        //判断id是否存在
        if (studentService.isExist(id)) {
            System.out.println("该id已存在，请重新输入");
            return;
        }

        Student student = getStudent(id);

        //调用service添加学生
        boolean ret = studentService.addStudent(student);
        if (ret) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
